﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonsLib.Utility;
using System.Data.SqlClient;
using MaxLeaf.Platform.Functions;
using System.Data.Common;
using MaxLeaf.Platform.Commons;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;

namespace MaxLeaf.Platform.DALImplementation
{
    public class imOthersInputDtlDAL : BaseDAL
    {



        public void SetRecvDtlId(DBAccess dbcon, int intKey, int intRecvHdrID, int intRecvDtlID)
        {
            string strSql = "update imOthersInputDtl set RecvHdrId=@RecvHdrId,RecvDtlId=@RecvDtlId where DtlID=@FInterID  ";
            dbcon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FInterID", intKey.ToString())
                ,new SqlParameter("@RecvHdrId", intRecvHdrID.ToString())
                ,new SqlParameter("@RecvDtlId", intRecvDtlID.ToString())
            });
        }

        public void SetPosted(DBAccess dbcon, int intKey)
        {
            string strSql = @"update imOthersInputDtl set  posted=1  where DtlID=@FInterID and posted=0";
            dbcon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FInterID", intKey.ToString())
 
            });
        }

        public List<int> GetUnpostKeys(DBAccess dbcon, int intHdrID)
        {
            List<int> lst = new List<int>();
            string strSql = "select DtlID from imOthersInputDtl where HdrID=@HdrID and posted=0 and is_delete=0 ";
            using (DbDataReader rdr = dbcon.GetDataReader(strSql, new SqlParameter[] { new SqlParameter("@HdrID", intHdrID.ToString())
            }))
            {
                while (rdr.Read())
                {
                    lst.Add(rdr.GetInt32(0));
                }

            }

            return lst;
        }

        public void SetUnRecv(DBAccess dbcon, int intKey)
        {
            string strSql = @"update imOthersInputDtl set  is_delete=1  where DtlID=@FInterID and posted=0";
            dbcon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FInterID", intKey.ToString())
 
            });
        }

        public void GetRecvData(DBAccess dbcon, int intKey, out int intRecvHdrId, out int intRecvDtlId, out double dblRecvQty)
        {
            intRecvHdrId = 0;
            intRecvDtlId = 0;
            dblRecvQty = 0;
            string strSql = "select RecvHdrId,RecvDtlId,ReqQty from imOthersInputDtl where DtlID=@DtlID ";
            using (DbDataReader rdr = dbcon.GetDataReader(strSql, new SqlParameter[] { new SqlParameter("@DtlID", intKey.ToString())
            }))
            {
                if (rdr.Read())
                {
                    intRecvHdrId=rdr.GetInt32(0);
                    intRecvDtlId = rdr.GetInt32(1);
                    dblRecvQty =CvtFunc.ToDouble( rdr[2]);
                }

            }
        }
    }
}
